<template>
  <div class="app-container">
    <el-form :inline="true" :model="page" class="demo-form-inline" size="mini">
      <el-form-item label="工单号">
        <el-input v-model="page.name" placeholder="工单号" />
      </el-form-item>
      <el-form-item label="工单分类">
        <el-select v-model="page.category" placeholder="工单分类">
          <el-option label="活动问题" value="活动问题" />
          <el-option label="账号问题" value="账号问题" />
          <el-option label="积分问题" value="积分问题" />
          <el-option label="功能建议" value="功能建议" />
          <el-option label="订单问题" value="订单问题" />
          <el-option label="海外集货转运问题" value="海外集货转运问题" />
          <el-option label="其他问题" value="其他问题" />
          <el-option label="全部" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSearch">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="listLoading"
      :data="list"
      size="mini"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        type="index"
        label="ID"
        width="50"
        align="center"
      />
      <el-table-column label="工单号" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="工单分类" align="center">
        <template slot-scope="scope">
          {{ scope.row.category }}
        </template>
      </el-table-column>
      <el-table-column label="问题" align="center">
        <template slot-scope="scope">
          {{ scope.row.summary }}
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center">
        <template slot-scope="scope">
          <el-image
            v-for="(img,index) in scope.row.images.split(',')"
            :key="index"
            style="width: 20px; height: 20px"
            :src="img"
            lazy
            :preview-src-list="scope.row.images.split(',')"
          />
        </template>
      </el-table-column>
      <el-table-column v-if="adminManager" label="处理人" align="center">
        <template slot-scope="scope">
          <el-select v-model="scope.row.handler" placeholder="请选择" size="mini" :disabled="scope.row.handler.length !==0 || scope.row.closed || scope.row.resolved" @change="handleAssign(scope.row)">
            <el-option
              v-for="item in optionAdmin"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-rate v-model="scope.row.star" :disabled="true" />
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="!scope.row.handler" type="danger" size="mini">
            未分配处理人
          </el-tag>
          <el-tag v-if="scope.row.handler && !scope.row.closed" type="primary" size="mini">
            处理中
          </el-tag>
          <el-tag v-if="scope.row.handler && scope.row.closed && scope.row.resolved" type="info" size="mini">
            已解决
          </el-tag>
          <el-tag v-if="scope.row.handler && scope.row.closed && !scope.row.resolved" type="warning" size="mini">
            未解决
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center">
        <template slot-scope="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" :disabled="scope.row.handler.length===0" @click="handlerOrder(scope.row.id)">处理</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="margin-top: 20px"
      :current-page.sync="page.current"
      :page-sizes="[5,10,20,50]"
      :page-size="page.size"
      layout="total,sizes, prev, pager, next"
      :total="page.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="centerDialogVisible"
      width="40%"
      center
    >
      <el-form>
        <el-divider content-position="center">工单信息</el-divider>
        <el-form-item label="工单号" :label-width="formLabelWidth">
          <el-input v-model="admin.name" autocomplete="off" size="mini" />
        </el-form-item>
        <el-form-item label="工单分类" :label-width="formLabelWidth">
          <el-input v-model="admin.category" autocomplete="off" size="mini" />
        </el-form-item>
        <el-form-item label="手机号码" :label-width="formLabelWidth">
          <el-input v-model="admin.mobile" autocomplete="off" size="mini" />
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="admin.mail" autocomplete="off" size="mini" />
        </el-form-item>
        <el-divider content-position="center">学校信息</el-divider>
        <el-form-item label="学校名称" :label-width="formLabelWidth">
          <el-input v-model="admin.schoolName" autocomplete="off" size="mini" />
        </el-form-item>
        <el-form-item label="学校类型" :label-width="formLabelWidth">
          <el-radio-group v-model="admin.type">
            <el-radio label="0">普通学校</el-radio>
            <el-radio label="1">培训机构</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="详细地址" :label-width="formLabelWidth">
          <el-input v-model="admin.addr" autocomplete="off" size="mini" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleNewSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { workOrderList, mangerSelect, workOrderAssign, adminStatus, adminEdit, adminAdd, initPassword } from '@/api/admin'
export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: [],
      listLoading: true,
      centerDialogVisible: false,
      formLabelWidth: '80px',
      dialogTitle: '新增工单',
      adminManager: this.$store.state.user.manager,
      admin: {
        id: -1,
        name: '',
        username: '',
        mobile: '',
        mail: '',
        schoolId: -1,
        prov: '',
        city: '',
        dist: '',
        addr: '',
        schoolName: '',
        type: 0,
        selectedOptions: []
      },
      optionAdmin: [],
      page: {
        name: '',
        category: '',
        size: 10,
        current: 1,
        total: 1
      }
    }
  },
  created() {
    console.log(this.$store.state.user.manager)
    mangerSelect().then(res => {
      this.optionAdmin = res.body
      this.fetchData()
    })
  },
  methods: {
    statusChange(row) {
      const title = row.status ? '是否确认启动该工单' : '是否确认禁用该工单'
      this.$confirm(title, '删除标签', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        adminStatus({ id: row.id, status: row.status }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      })
    },
    handleCommand(val) {
      if (val['op'] === 'init') {
        this.handleInit(val['data'])
      } else if (val['op'] === 'delete') {
        this.handleDelete(val['data'])
      }
    },
    handleChange(value) {
    },
    handleAssign(row) {
      this.listLoading = true
      workOrderAssign(row).then(res => {
        this.$message({
          type: 'success',
          message: '分配成功'
        })
        this.listLoading = false
      }).catch(err => {
        row.handler = ''
        this.$message({
          type: 'waring',
          message: err
        })
        this.listLoading = false
      })
    },
    fetchData() {
      this.listLoading = true
      workOrderList(this.page).then(response => {
        this.list = response.body
        this.page.total = response.count
        this.listLoading = false
      })
    },
    handleAdd() {
      this.dialogTitle = '新增工单'
      this.admin.name = ''
      this.admin.username = ''
      this.admin.mobile = ''
      this.admin.mail = ''
      this.admin.name = ''
      this.admin.schoolId = -1
      this.admin.schoolName = ''
      this.admin.prov = ''
      this.admin.city = ''
      this.admin.dist = ''
      this.admin.addr = ''
      this.admin.type = '0'
      this.centerDialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑工单'
      this.admin.id = row.id
      this.admin.name = row.name
      this.admin.username = row.username
      this.admin.mobile = row.mobile
      this.admin.mail = row.mail
      this.admin.schoolName = row.schoolName
      this.admin.schoolId = row.schoolId
      this.admin.prov = row.prov
      this.admin.city = row.city
      this.admin.dist = row.dist
      this.admin.addr = row.addr
      this.admin.type = row.type.toString()
      console.log(this.admin)
      this.centerDialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('是否确认删除该工单', '删除标签', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        adminStatus({ id: row.id }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      })
    },
    handleInit(row) {
      this.$confirm('是否确认初始化该工单的密码', '初始化密码', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        initPassword({ id: row.id }).then(response => {
          if (response.code === 200) {
            this.fetchData()
            this.$message({
              type: 'success',
              message: '初始化成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      })
    },
    handleNewSave() {
      if (this.admin.id === -1) {
        adminAdd(this.admin).then(response => {
          if (response.code === 200) {
            this.centerDialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '工单添加成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      } else {
        adminEdit(this.admin).then(response => {
          if (response.code === 200) {
            this.centerDialogVisible = false
            this.fetchData()
            this.$message({
              type: 'success',
              message: '工单编辑成功'
            })
          } else {
            this.$message({
              type: 'waring',
              message: response.message
            })
          }
        })
      }
    },
    handleSizeChange(val) {
      this.page.size = val
      this.fetchData()
    },
    onSearch() {
      this.fetchData()
    },
    handlerOrder(workOrderid) {
      this.$router.push({ path: '/workorder/comments', query: { orderid: workOrderid }})
    },
    handleCurrentChange(val) {
      this.page.current = val
      this.fetchData()
    }
  }
}
</script>
<style>
  .item {
    margin-top: 10px;
    margin-right: 40px;
  }
</style>
